package com.gimi.cloud.bbp.task.controller;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.log.LogScheduleJobDTO;
import com.gimi.cloud.bbp.task.service.ScheduleJobLogService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ${DESCRIPTION}
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-02-06
 **/
@RestController
@RequestMapping("/task/log")
public class ScheduleJobLogController {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobLogController.class);
    private ScheduleJobLogService scheduleJobLogService;

    @Autowired
    public ScheduleJobLogController(ScheduleJobLogService scheduleJobLogService) {
        this.scheduleJobLogService = scheduleJobLogService;
    }

    /**
     * 获取定时任务执行日志
     * @param request
     * @param response
     * @param dto 查询参数
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response, @RequestBody LogScheduleJobDTO dto) throws Exception{
        Response resp = new Response();
        List<LogScheduleJobDTO> logScheduleJobs =  scheduleJobLogService.queryList(dto,true);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,new PageInfo<>(logScheduleJobs));
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 添加日志
     * @param request
     * @param response
     * @param dto 待添加的日志
     * @throws Exception
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,HttpServletResponse response,@RequestBody LogScheduleJobDTO dto) throws  Exception{
        scheduleJobLogService.save(dto);
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"保存成功");
        Util.sendResponse(JsonUtil.toJson(resp),response);
    }
}
